<!-- subpkg_consult/payment/payment.vue -->
<script lang="ts" setup>
  import { createConsultOrderAPI, getConsultOrderPayUrlAPI, getConsultOrderPreAPI } from '@/api/consult'
  import { getPatientDetailAPI } from '@/api/patients'
  import { useConsultStore } from '@/stores/consult'
  import type { OrderPreData } from '@/types/consult'
  import type { PatientItem } from '@/types/patient'
  import { onLoad } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  // 预付订单信息
  const preOrderInfo = ref({} as OrderPreData)
  //获取仓库
  const store = useConsultStore()
  onLoad(async () => {
    // 获取预付订单信息
    const res = await getConsultOrderPreAPI({ type: store.type, illnessType: store.illnessType })
    preOrderInfo.value = res
  })

  //获取患者信息
  const patientInfo = ref({} as PatientItem)
  onLoad(async () => {
    const res = await getPatientDetailAPI(store.patientId)
    patientInfo.value = res
  })

  // 支付弹窗
  const paymentRef = ref()
  const orderId = ref('')
  const onSelectPayment = async () => {
    const { id } = await createConsultOrderAPI({
      type: store.type,
      illnessType: store.illnessType,
      patientId: store.patientId,
      depId: store.depId,
      illnessDesc: store.illnessInfo.illnessDesc,
      illnessTime: store.illnessInfo.illnessTime,
      consultFlag: store.illnessInfo.consultFlag,
      pictures: store.illnessInfo.pictures,
    })
    //生成订单之后清空store的患者数据
    store.clear()
    orderId.value = id
    paymentRef.value.open()
  }

  // 关闭支付弹窗
  const onCancel = async () => {
    const { confirm } = await uni.showModal({
    title: '关闭支付',
    content: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    cancelText: '仍要关闭',
    cancelColor: '#848484',
    confirmText: '继续支付',
    confirmColor: '#16C2A3',
  })
    if(!confirm){
      paymentRef.value.close()
      uni.reLaunch({ url: '/pages/index/index' })
    }  
  }

  // 开始支付
  const onPay = async (channelIndex:number) => {
    //判断选择的支付方法
    if (channelIndex === 0) {
      uni.showToast({
        title: '当前版本暂不支持微信支付',
        icon: 'none',
      })
      return
    }
    // 调用接口获取支付地址
    const res = await getConsultOrderPayUrlAPI({
      orderId: orderId.value,
      paymentMethod: channelIndex + '',
      payCallback: 'http://localhost:5173/#/subpkg_consult/room/room',
    })

    // 跳转到支付页面
    window.location.href = res.payUrl
  }
</script>

<template>
  <scroll-page>
    <view class="payment-page">
      <uni-section
        title-font-size="32rpx"
        title-color="#121826"
        padding="30rpx"
        :title="`图文问诊 ${preOrderInfo.payment}元`"
      >
        <uni-list :border="false">
          <uni-list-item
            title="极速问诊"
            note="自动分配医生"
            thumb="/static/uploads/doctor-avatar.jpg"
            thumb-size="lg"
          />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="`-¥${preOrderInfo.couponDeduction}`"
          />
          <uni-list-item title="积分抵扣">
            <template #footer>
              <view class="uni-list-text-red">-¥{{ preOrderInfo.pointDeduction }}</view>
            </template>
          </uni-list-item>
          <uni-list-item title="实付款">
            <template #footer>
              <view class="uni-list-text-red">¥{{ preOrderInfo.actualPayment }}</view>
            </template>
          </uni-list-item>
        </uni-list>
      </uni-section>

      <view class="dividing-line"></view>

      <uni-section title-font-size="32rpx" title-color="#121826" padding="30rpx" title="患者资料">
        <uni-list :border="false">
          <uni-list-item title="患者信息">
            <template #footer>
              <view class="uni-list-text-gray">
                {{ patientInfo.name }} | {{ patientInfo.gender ? '男' : '女' }} |
                {{ patientInfo.age }}岁
              </view>
            </template>
          </uni-list-item>
          <uni-list-item border title="病情描述" :note="`${store.illnessInfo.illnessDesc}`" />
        </uni-list>
      </uni-section>

      <!-- <view class="payment-agreement">
        <radio color="#20c6b2" value="1" />
        我已同意<text style="color: #20c6b2">支付协议</text>
      </view> -->
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <view class="total-amount">
        合计: <text class="number">¥{{ preOrderInfo.actualPayment }}</text>
      </view>
      <button class="uni-button" @click="onSelectPayment">立即支付</button>
    </view>

    <!-- 支付弹窗 -->
    <custom-payment
      :orderId="orderId"
      :amount="preOrderInfo.actualPayment"
      ref="paymentRef"
      @onClose="onCancel"
      @on-pay="onPay"
    />
  </scroll-page>
</template>

<style lang="scss">
  @import './payment.scss';
</style>
